package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.mapper.AdminOrderMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.service.AdminOrderService;
import com.sky.vo.HistoryOrders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    AdminOrderMapper adminOrderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Override
    public HistoryOrders historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        HistoryOrders historyOrders = new HistoryOrders();
        PageHelper.startPage(ordersPageQueryDTO.getPage(),ordersPageQueryDTO.getPageSize());
        List<OrderVO> orderVO = adminOrderMapper.historyOrders(ordersPageQueryDTO);
        for (OrderVO vo : orderVO) {
            Long id = vo.getId();
            vo.setOrderDetailList(orderDetailMapper.getOrderDetailListByOrderId(id));
        }
        historyOrders.setRecords(orderVO);
        historyOrders.setTotal(adminOrderMapper.historyOrders(ordersPageQueryDTO).size());
        System.out.println(historyOrders.getTotal());
        return historyOrders;
    }
    @Override
    public OrderStatisticsVO statistics() {
        //订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消 7退款
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO.setConfirmed(adminOrderMapper.getConfirmedOrdersCount());
        orderStatisticsVO.setDeliveryInProgress(adminOrderMapper.getDeliveryInProgressCount());
        orderStatisticsVO.setToBeConfirmed(adminOrderMapper.getToBeConfirmedCount());
        return orderStatisticsVO;
    }

    public OrderVO getdetail(String id) {
        OrderVO orderVO=adminOrderMapper.selectById(id);
        List<OrderDetail> orderDetail=orderDetailMapper.getOrderDetailListByOrderId(orderVO.getId());
        orderVO.setOrderDetailList(orderDetail);
        LocalDateTime estimatedDeliveryTime = orderVO.getEstimatedDeliveryTime();
        return orderVO;
    }

    @Override
    public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
        adminOrderMapper.confirm(ordersConfirmDTO);
    }

    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
        adminOrderMapper.rejection(ordersRejectionDTO);
    }

    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        adminOrderMapper.cancel(ordersCancelDTO);
    }

    @Override
    public void delivery(String id) {
        adminOrderMapper.delivery(id);
    }

    @Override
    public void complete(String id) {
        adminOrderMapper.complete(id);
    }
}
